package com.omenkk.juc.AQS;

import com.omenkk.juc.util.Slepper;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
@Slf4j
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier barrier=new CyclicBarrier(2);
        new Thread(()->{
            log.debug("线程1开始执行");
            try{
                barrier.await();
            }catch (InterruptedException| BrokenBarrierException e){
                e.printStackTrace();
            }
           log.debug("线程1同步完成，继续执行");

        }).start();

        new Thread(()->{
            log.debug("线程2开始执行");
            try{
                Slepper.sleep(1);
                barrier.await();
            }catch (InterruptedException| BrokenBarrierException e){
                e.printStackTrace();
            }
            log.debug("线程2同步完成，继续执行");

        }).start();


        Slepper.sleep(3);

        new Thread(()->{
            log.debug("线程3开始执行");
            try{
                barrier.await();
            }catch (InterruptedException| BrokenBarrierException e){
                e.printStackTrace();
            }
            log.debug("线程3同步完成，继续执行");

        }).start();

        new Thread(()->{
            log.debug("线程4开始执行");
            try{
                Slepper.sleep(1);
                barrier.await();
            }catch (InterruptedException| BrokenBarrierException e){
                e.printStackTrace();
            }
            log.debug("线程4同步完成，继续执行");

        }).start();
    }
}
